package com.stephentuso.welcome;

import com.stephentuso.welcome.util.PageView;
import com.stephentuso.welcome.util.ResUtil;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.utils.PacMap;

/**
 * Created by stephentuso on 11/15/15.
 * A simple Fraction that shows an image, a heading, and a description.
 */
public class WelcomeBasicPageView extends PageView implements WelcomePage.OnChangeListener {

    public static final String KEY_DRAWABLE_ID = "drawable_id";
    public static final String KEY_DESCRIPTION = "description";
    public static final String KEY_TITLE = "title";
    public static final String KEY_SHOW_ANIM = "show_anim";
    public static final String KEY_HEADER_TYPEFACE_PATH = "header_typeface";
    public static final String KEY_DESCRIPTION_TYPEFACE_PATH = "description_typeface";
    public static final String KEY_HEADER_COLOR = "header_color";
    public static final String KEY_DESCRIPTION_COLOR = "description_color";

    private Image imageView = null;
    private Text titleView = null;
    private Text descriptionView = null;
    private boolean showParallaxAnim = true;

    public static WelcomeBasicPageView newInstance(Context context,
                                                   int drawableId,
                                                   String title,
                                                   String description,
                                                   boolean showParallaxAnim,
                                                   String headerTypefacePath,
                                                   String descriptionTypefacePath,
                                                   int headerColor,
                                                   int descriptionColor)
    {
        PacMap args = new PacMap();
        args.putIntValue(KEY_DRAWABLE_ID, drawableId);
        args.putString(KEY_TITLE, title);
        args.putString(KEY_DESCRIPTION, description);
        args.putBooleanValue(KEY_SHOW_ANIM, showParallaxAnim);
        args.putString(KEY_HEADER_TYPEFACE_PATH, headerTypefacePath);
        args.putString(KEY_DESCRIPTION_TYPEFACE_PATH, descriptionTypefacePath);
        args.putIntValue(KEY_HEADER_COLOR, headerColor);
        args.putIntValue(KEY_DESCRIPTION_COLOR, descriptionColor);
        WelcomeBasicPageView pageView = new WelcomeBasicPageView(context, args);
        return pageView;
    }

    public WelcomeBasicPageView(Context context, PacMap args) {
        super(context);
        Component component = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_wel_page_view_basic, this, true);

        imageView = (Image) component.findComponentById(ResourceTable.Id_wel_image);
        titleView = (Text) component.findComponentById(ResourceTable.Id_wel_title);
        descriptionView = (Text) component.findComponentById(ResourceTable.Id_wel_description);

        showParallaxAnim = args.getBooleanValue(KEY_SHOW_ANIM, showParallaxAnim);

        imageView.setImageElement(ResUtil.getVectorDrawable(context, args.getIntValue(KEY_DRAWABLE_ID)));

        if (args.getString(KEY_TITLE) != null)
            titleView.setText(args.getString(KEY_TITLE));

        if (args.getString(KEY_DESCRIPTION) != null)
            descriptionView.setText(args.getString(KEY_DESCRIPTION));

        int headerColor = args.getIntValue(KEY_HEADER_COLOR, WelcomeUtils.NO_COLOR_SET);
        if (headerColor != WelcomeUtils.NO_COLOR_SET)
            titleView.setTextColor(new Color(headerColor));

        int descriptionColor = args.getIntValue(KEY_DESCRIPTION_COLOR, WelcomeUtils.NO_COLOR_SET);
        if (descriptionColor != WelcomeUtils.NO_COLOR_SET)
            descriptionView.setTextColor(new Color(descriptionColor));

        WelcomeUtils.setTypeface(titleView, args.getString(KEY_HEADER_TYPEFACE_PATH), getContext());
        WelcomeUtils.setTypeface(descriptionView, args.getString(KEY_DESCRIPTION_TYPEFACE_PATH), getContext());
    }

    @Override
    public void onWelcomeScreenPageScrolled(int pageIndex, float offset, int offsetPixels) {
        /*if (showParallaxAnim && imageView != null) {
            imageView.setTranslationX(-offsetPixels * 0.8f);
        }*/
    }

    @Override
    public void onWelcomeScreenPageSelected(int pageIndex, int selectedPageIndex) {
        //Not used
    }

    @Override
    public void onWelcomeScreenPageScrollStateChanged(int pageIndex, int state) {
        //Not used
    }
}
